# Flores, Alejandro and Alexander Coppock
# Do Bilinguals Respond More Favorably to Candidate Advertisements in English or in Spanish?
# Forthcoming, Political Communication

rm(list = ls())

# Set your working directory to the replication archive directory

# Install software if necessary
# install.packages(c("stargazer", "tidyverse", "margins"))
# devtools::install_github("acoppock/commarobust")

library(stargazer)
library(tidyverse)
library(margins)

study_1 <- read_csv("study_1.csv")
study_2 <- read_csv("study_2.csv")
study_3 <- read_csv("study_3.csv")

study_1 <- study_1 %>% mutate(Z_ad = as.numeric(Z_ad == "spanish_ad"))


# Table C.9 ---------------------------------------------------------------

fit_0_b <- glm(bush_general ~ Z_ad + Z_survey, family = "binomial", data = filter(study_1, bilingual == 1))
fit_1_b <- glm(vela_general ~ Z_ad + Z_survey, family = "binomial", data = (filter(study_2, bilingual == 1) %>% mutate(Z_ad = Z_vela)))
fit_1_m <- glm(vela_general ~ Z_ad, family = "binomial", data = (filter(study_3, bilingual == 0) %>% mutate(Z_ad = Z_vela)))
fit_2_b <- glm(coffman_general ~ Z_ad + Z_survey, family = "binomial", data = (filter(study_2, bilingual == 1) %>% mutate(Z_ad = Z_coffman)))
fit_2_m <- glm(coffman_general ~ Z_ad, family = "binomial", data = (filter(study_3, bilingual == 0) %>% mutate(Z_ad = Z_coffman)))

stargazer(fit_0_b, fit_1_b, fit_1_m, fit_2_b, fit_2_m,
          covariate.labels = c("Spanish-language Ad", "Spanish-language Survey", "Constant"),
          dep.var.labels = c("Bush: General Election", "Vela: General Election", "Coffman: General Election"),
          add.lines = list(c("Sample", "Bilingual", "Bilingual", "Monolingual", "Bilingual", "Monolingual")),
          style = "apsr",
          omit.stat = c("f", "ser", "adj.rsq", "rsq"),
          title = "Effect of Spanish-language Ad on General Election Support (Logit)",
          label = "general_outcomes",
          column.sep.width = "0pt",
          font.size = "footnotesize")

margins(fit_0_b)
margins(fit_1_b)
margins(fit_1_m)
margins(fit_2_b)
margins(fit_2_m)



# Table C.10 --------------------------------------------------------------

fit_6_b <- glm(bush_cares ~ Z_ad + Z_survey, family = "binomial", data = filter(study_1, bilingual == 1))
fit_7_b <- glm(vela_cares ~ Z_ad + Z_survey, family = "binomial", data = (filter(study_2, bilingual == 1) %>% mutate(Z_ad = Z_vela)))
fit_7_m <- glm(vela_cares ~ Z_ad, family = "binomial", data = (filter(study_3, bilingual == 0) %>% mutate(Z_ad = Z_vela)))
fit_8_b <- glm(coffman_cares ~ Z_ad + Z_survey, family = "binomial", data = (filter(study_2, bilingual == 1) %>% mutate(Z_ad = Z_coffman)))
fit_8_m <- glm(coffman_cares ~ Z_ad, family = "binomial", data = (filter(study_3, bilingual == 0) %>% mutate(Z_ad = Z_coffman)))

stargazer(fit_6_b, fit_7_b, fit_7_m, fit_8_b, fit_8_m,
          covariate.labels = c("Spanish-language Ad", "Spanish-language Survey", "Constant"),
          dep.var.labels = c("Bush Cares", "Vela Cares", "Coffman Cares"),
          add.lines = list(c("Sample", "Bilingual", "Bilingual", "Monolingual", "Bilingual", "Monolingual")),
          style = "apsr",
          omit.stat = c("f", "ser", "adj.rsq", "rsq"),
          title = "Effect of Spanish-language Ad on Perceptions of Candidate Caring (Logit)",
          label = "care_outcomes",
          column.sep.width = "0pt",
          font.size = "footnotesize")

margins(fit_6_b)
margins(fit_7_b)
margins(fit_7_m)
margins(fit_8_b)
margins(fit_8_m)


